package com.yongcheon.sms_manager.data.preference;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.utils.StringUtil;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-09.
 */
public class SpamNumberData {
    Context context;

    public SpamNumberData(Context context){
        this.context = context;
    }

    public ArrayList<SpamNumVO> selectReturnVo(String poneNum, Context context, SQLiteDatabase database){
        Cursor cursor = select(poneNum, context, database);
        return getSpamNumVo(cursor);
    }

    public Cursor select(String poneNum, Context context, SQLiteDatabase database){
        String sql = "Select * from spam_number";
        if(poneNum != null) sql = sql + " Where address = '" + poneNum+"'";
        sql = sql + " order by address desc";
        Cursor cursor = database.rawQuery(sql, null);
        return cursor;
    }

    public ArrayList<SpamNumVO> getSpamNumVo(Cursor cursor){
        ArrayList<SpamNumVO> spamVoList = new ArrayList<SpamNumVO>();
        if(cursor != null) {
            while (cursor.moveToNext()) {
                SpamNumVO spamVo = new SpamNumVO();
                spamVo.setAddress(cursor.getString(cursor.getColumnIndex("address")));
                spamVo.setCategory_1(cursor.getString(cursor.getColumnIndex("category_1")));
                spamVo.setCategory_2(cursor.getString(cursor.getColumnIndex("category_2")));
                spamVo.setCategory_3(cursor.getString(cursor.getColumnIndex("category_3")));
                spamVo.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
                spamVo.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
                spamVo.setEtc(cursor.getString(cursor.getColumnIndex("etc")));
                spamVoList.add(spamVo);
            }
            cursor.close();
        }
        return spamVoList;
    }

    public SpamNumVO createSpamNumVOFromCursor(Cursor cursor){
        String[] names = cursor.getColumnNames();
        SpamNumVO spamVo = new SpamNumVO();
        spamVo.set_id(cursor.getLong(cursor.getColumnIndex("_id")));
        spamVo.setAddress(cursor.getString(cursor.getColumnIndex("address")));
        spamVo.setCategory_1(cursor.getString(cursor.getColumnIndex("category_1")));
        spamVo.setCategory_2(cursor.getString(cursor.getColumnIndex("category_2")));
        spamVo.setCategory_3(cursor.getString(cursor.getColumnIndex("category_3")));
        spamVo.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
        spamVo.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
        spamVo.setEtc(cursor.getString(cursor.getColumnIndex("etc")));
        return spamVo;
    }

    public void insertNotExist(SpamNumVO spamVo, Context context, SQLiteDatabase database){
        ArrayList<SpamNumVO> spamNumVoList = selectReturnVo(spamVo.getAddress(), context, database);
        if(spamNumVoList.size() == 0){
            insert(spamVo, context, database);
        }
    }


    public void insert(SpamNumVO vo, Context context, SQLiteDatabase database){
        String sql = "Insert into spam_number (address, category_1, category_2, category_3, subject, desc, etc) values "+
                "('"+vo.getAddress()+"','"+vo.getCategory_1()+"','"+vo.getCategory_2()+"','"+vo.getCategory_3()+"','"+ StringUtil.changeSpecialLettersForSQL(vo.getSubject())+"','"+StringUtil.changeSpecialLettersForSQL(vo.getDesc())+"','"+vo.getEtc()+"')";
        database.execSQL(sql);
    }

    public void delete(String address, Context context, SQLiteDatabase database){
        String sql = "Delete From spam_number Where address = '"+address+"'";
        database.execSQL(sql);
    }

    public void update(SpamNumVO spamNumVO, Context context, SQLiteDatabase database){
        String sql = "Update spam_number set category_2 = '"+spamNumVO.getCategory_2()+"', subject = '"+StringUtil.changeSpecialLettersForSQL(spamNumVO.getSubject())+"' , address = '"+spamNumVO.getAddress()+"' Where address = '"+spamNumVO.getAddress()+"'";
        database.execSQL(sql);
    }

    public String getFrom(SpamNumVO spamNumVO, String poneNum, Context context){
        String from = "";
        if(spamNumVO != null){
            from = spamNumVO.getSubject() + " ( " + poneNum + " )";
        }else{
            from = context.getResources().getString(R.string.unregistered_number)  + " ( " + poneNum + " )";
        }
        return from;
    }
}
